<?php !defined('BASE_ROOT') && exit ( 'Access Denied!' );
class Access {

	protected $querynum = 0;
	protected $connstring;

	public function __construct($dbfile,$username,$password,$pconnect=0){
		$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath($dbfile);
		if($pconnect) {
			if(!$this->connstring = odbc_connect($connstr,$username,$password,SQL_CUR_USE_ODBC)){
				show_error('无法连接到数据库');
			}			
		}else{
			if(!$this->connstring = odbc_pconnect($connstr,$username,$password,SQL_CUR_USE_ODBC)){
				show_error('无法连接到数据库');
			}				
		}
		return $this->connstring;
	}

	public function query($sql) {

		if(!@odbc_exec($this->connstring,$sql)){
			show_error('查询失败！<br/>'.$this->getError().'<br/>SQL:'.$sql);
		}
		$this->querynum++;
		return $result;
	}

	public function fetch_array($result){
		return odbc_fetch_array($result);
	}

	public function fetch_assoc($result){//无这个函数
		return;
	}

	public function fetch_row($result) {     
		return odbc_fetch_row($result);  
	}

	public function num_fields($result){
		return odbc_num_fields($result);
	}

	public function num_rows($result){
		return odbc_num_rows($result);
	}

	public result($result,$num){
		return odbc_result($result,$num)
	}

	public function insert_id() {
		return sqlite_last_insert_rowid($this->connstring);
	}

	public function rows_count($sql){
		$result = $this->query($sql);
		$rowsnum = $this->num_rows($result);
		$this->querynum++;
		return $rowsnum;
	}

	public function affected_rows() {//无效函数
		return;  
	}

	public function free_result($result){//sqlite无相应的函数
		return odbc_free_result($result);
	}

	public function getOne($sql) {
		$result = $this->query($sql);
		$row = $this->fetch_array($result);
		$this->querynum++;
		$this->free_result($result);
		return array_shift($row);
	}

	public function getRow($sql){
		$result = $this->query($sql);
		$data = $this->fetch_array($result);
		$this->querynum++;
		$this->free_result($result);
		return $data;
	}

	public function getAll($sql){
		$arr = array();
		$result = $this->query($sql);

//　　　　if($this->fetch_row($result))
//　　　　{
//　　　　　　for($i=1;$i<$colnum;$i++)
//　　　　　　{
//　　　　　		$info[$i]=odbc_result($query,$i);
//　　　　　　}
//　　　　}
//　　　　return $info;

		while($data = $this->fetch_array($result)) {
			$arr[] = $data;
		}
		$this->querynum++;
		$this->free_result($result);
		return $arr;
	}

	//添加数据
	public function insert($tablename, $insertsqlarr, $returnid=0, $replace = false, $silent=0) {
		$insertkeysql = $insertvaluesql = $comma = '';
		foreach ($insertsqlarr as $insert_key => $insert_value) {
			$insertkeysql .= $comma.$insert_key;
			$insertvaluesql .= $comma.'\''.$insert_value.'\'';
			$comma = ', ';
		}
		$method = $replace?'REPLACE':'INSERT';
		$this->query($method.' INTO '.$tablename.' ('.$insertkeysql.') VALUES ('.$insertvaluesql.')', $silent?'SILENT':'');
		if($returnid && !$replace) {
			return $this->insert_id();
		}
	}

	//更新数据
	public function update($tablename, $setsqlarr, $wheresqlarr, $silent=0) {
		$setsql = $comma = '';
		foreach ($setsqlarr as $set_key => $set_value) {
			$setsql .= $comma.$set_key.'=\''.$set_value.'\'';
			$comma = ', ';
		}
		$where = $comma = '';
		if(empty($wheresqlarr)) {
			$where = '1';
		} elseif(is_array($wheresqlarr)) {
			foreach ($wheresqlarr as $key => $value) {
				$where .= $comma.$key.'=\''.$value.'\'';
				$comma = ' AND ';
			}
		} else {
			$where = $wheresqlarr;
		}
		$this->query('UPDATE '.$tablename.' SET '.$setsql.' WHERE '.$where, $silent?'SILENT':'');
	}
	//删除数据
	public function delete($tablename, $var='id', $range) {
		if($range) {
			if(is_array($range)){
				$range = implode(',',$id);
			}
			$this->query('DELETE FROM '.$tablename.' WHERE '.$var.' in ('.$range.')');
		}
	}

	public function close(){
		if(isset($this->connstring)){
			odbc_close($this->connstring);
		}
	}

	public function getversion(){//无该函数
		return ;
	}

	public function getQueryNum() {
		return $this->querynum;
	}

	public function getError() {
		return odbc_errormsg($this -> connstring);
	}
}